package com.tool_stdy.IO;

import java.nio.IntBuffer;

/**
 * Created by yan_li on 2017/1/6.
 */
public class BufferTest {

    public static void intBufferTest() {
        IntBuffer buf = IntBuffer.allocate(10);
        buf.put(13);
        buf.put(21);
        buf.put(35);
        //把游标位置复位为0,如果不执行此操作的话游标位置是3
        buf.flip();
        System.out.println(buf);
        System.out.println("容量:" + buf.capacity());
        System.out.println("限制为:" + buf.limit());
        System.out.println("下标为1:" + buf.get(1));//游标从零开始
        System.out.println("get(index)方法,position位置不变:" + buf);
        buf.put(1, 4);
        System.out.println("put(index,change)方法,position位置不变:" + buf);
        System.out.println("下标为1:" + buf.get(1));//游标从零开始
        for (int i = 0; i < buf.limit(); i++) {
            //调用get方法会使其缓冲区位置(position)向后递增一位
            System.out.println(buf.get());
        }
        System.out.println( buf.position());
        System.out.println("get(index)方法,position位置不变:" + buf);

    }

    public static void main(String[] args) {
        intBufferTest();
    }
}
